package com.bw.educate.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.educate.common.ResponseResult;
import com.bw.educate.common.auth.UserInfo;
import com.bw.educate.entity.EduCourseBase;
import com.bw.educate.entity.EduUserCourse;
import com.bw.educate.service.IEduUserCourseService;
import com.bw.educate.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 在线教育-五组
 * @since 2020-08-12
 */
@RestController
@RequestMapping("eduUserCourse")
public class EduUserCourseController {

    @Autowired
    private IEduUserCourseService eduUserCourseService;

    @Autowired
    private UserUtil userUtil;

    /**
     * @Description:学员授权保存
     * @Author: 周明丽
     * @Date: 2020/8/25 11:44
     * @param courseId:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("addSave/{courseId}")
    public ResponseResult addSave(@PathVariable("courseId") Long courseId, HttpServletRequest request){
        ResponseResult result=ResponseResult.SUCCESS();
        try {
            UserInfo loginUser = userUtil.getLoginUser(request);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("USER_ID",loginUser.getId());
            queryWrapper.eq("COURSE_ID",courseId);
            List<EduUserCourse> list = eduUserCourseService.list(queryWrapper);
            if(list.isEmpty()){
                eduUserCourseService.addSave(courseId,loginUser.getId());
            }
            result.setMessage("保存成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("学员课程授权保存失败"+e.toString());
        }
        return result;
    }

    @GetMapping("list/{id}")
    public ResponseResult list(@PathVariable Long id){
        ResponseResult result=ResponseResult.SUCCESS();
        try {
            List<EduCourseBase> list=eduUserCourseService.getListById(id);
            result.setResult(list);
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
		}
        return result;
    }


	/**
     * @Description:根据课程id查询学习人数
     * @Author: 王文佳
     * @Date: 2020/9/2 10:55
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("getCourseById/{id}")
    public ResponseResult getCourseById(@PathVariable("id") Long id){
        ResponseResult result=ResponseResult.SUCCESS();
        try {
            Integer count = eduUserCourseService.getCourseById(id);
            result.setResult(count);
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
		}
        return result;
    }

    @GetMapping("addSaves/{courseId}")
    public String addSaves(@PathVariable("courseId") Long courseId, HttpServletRequest request) {

        UserInfo loginUser = userUtil.getLoginUser(request);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("USER_ID", loginUser.getId());
        queryWrapper.eq("COURSE_ID", courseId);
        List<EduUserCourse> list = eduUserCourseService.list(queryWrapper);
        if (list.isEmpty()) {
            eduUserCourseService.addSave(courseId, loginUser.getId());

        }
        return "保存成功";
    }

}
